Visualize state mentions reddit data

# Create an interactive plot that shows state mentions per state population
import plotly.express as px
import numpy as np
import pandas as pd

data = pd.read_csv('tmp/state_mentions_ranking.csv')
data = data.rename(columns={"mentions_per_pop": "mentions"})
fig = px.bar(data, x='states_mentioned', y='mentions', color_discrete_sequence=["#AC88FF"],
            template='plotly_white', hover_data={'mentions':':.2f',
                                                 'states_mentioned':False},
            labels={"mentions": "Mentions per 100,000 people",
                     "states_mentioned": ""})
fig.update_layout(xaxis={'categoryorder':'total descending'},
                 width=900,
                 height=600)
fig.show()
fig.write_html("../../website-source/plots/interactive_state_pop.html")
# Create interactive plots that show state mention ranks compared to other ranks
from plotly.subplots import make_subplots
import plotly.graph_objects as go

# Set figure dims
fig_2 = make_subplots(rows=1, cols=4)

# Change variable name
data = data.rename(columns={"states_mentioned": "State"})

# Create sub figures
# Population
fig_2.add_trace(
    px.scatter(data, y='mentions_rank', x='pop_rank', color_discrete_sequence=["#00C08D"] ,
            template='plotly_white', hover_data={'mentions_rank':':.0f',
                                                 'pop_rank':':.0f',
                                                 'State': True},
            labels={"mentions_rank": "Mentions Rank",
                     "pop_rank": "Population Rank"},
                 width=300,
                 height=300).data[0],
    row=1, col=1
)

# Visited
fig_2.add_trace(
    px.scatter(data, y='mentions_rank', x='visits_rank', color_discrete_sequence=['#FFA15A'] ,
            template='plotly_white', hover_data={'mentions_rank':':.0f',
                                                 'visits_rank':':.0f',
                                                 'State': True},
            labels={"mentions_rank": "Mentions Rank",
                     "pop_rank": "Visited Rank"},
                 width=300,
                 height=300).data[0],
    row=1, col=2
)

# US News Overall
fig_2.add_trace(
    px.scatter(data, y='mentions_rank', x='us_news_rank', color_discrete_sequence=["#00B4EF"] ,
            template='plotly_white', hover_data={'mentions_rank':':.0f',
                                                 'us_news_rank':':.0f',
                                                 'State': True},
            labels={"mentions_rank": "Mentions Rank",
                     "pop_rank": "US News Rank"},
                 width=300,
                 height=300).data[0],
    row=1, col=3
)

# US News Natural
fig_2.add_trace(
    px.scatter(data, y='mentions_rank', x='nature_rank', color_discrete_sequence=["#FF6C90"] ,
            template='plotly_white', hover_data={'mentions_rank':':.0f',
                                                 'nature_rank':':.0f',
                                                 'State': True},
            labels={"mentions_rank": "Mentions Rank",
                     "pop_rank": "US News Nature Rank"},
                 width=300,
                 height=300).data[0],
    row=1, col=4
)

fig_2.update_layout(
    height=350,
    width=900,
    template = 'plotly_white',
    yaxis=dict(title=dict(text='Mentions Rank')),
    xaxis1=dict(title=dict(text='Population Rank')),
    xaxis2=dict(title=dict(text='Visits Rank')),
    xaxis3=dict(title=dict(text='US News Rank')),
    xaxis4=dict(title=dict(text='US News Nature Rank')),
)

fig_2.show()
fig_2.write_html("../../website-source/plots/mentions_rank.html")